#!/bin/bash

. /usr/share/preupgrade/common.sh

#END GENERATED SECTION

##
# Long story short:
#
#    If you want to do some checks or automatic actions when execute
#    redhat-upgrade-tool, before the reboot, this is what you need to do.
#    Basically just copy executable files into the $POSTUPGRADE_SCRIPT.

###########################################################
# FUNCTIONS AND VARIABLES                                 #
###########################################################


#
# The name of the post-upgrade script. Change the name to the script you
# would like to execute during post-upgrade phase when redhat-upgrade-tool is
# executed. The script is expected to be in the same dir as the module.
# IMPORTANT:
#   Put here just the FILENAME. Do not put here path to the file - which is
#   supposed to be in the same directory as the module in this case.
#
POST_UPGRADE_SCRIPT="<POST_UPGRADE_SCRIPT>"

#
# Update this path as needed regarding the name of the module
#
_POSTUPGRADE_SCRIPT_DIR="$POSTUPGRADE_DIR/3rdparty/mycustom/05_add_post_upgrade_script"
_DST_POSTUPGRADE_SCRIPT_PATH="$_POSTUPGRADE_SCRIPT_DIR/$POST_UPGRADE_SCRIPT"

install_postupgrade_script() {
    mkdir -p "$_POSTUPGRADE_SCRIPT_DIR"
    cp -a "$POST_UPGRADE_SCRIPT" "$_DST_POSTUPGRADE_SCRIPT_PATH"
    chmod +x "$_DST_POSTUPGRADE_SCRIPT_PATH"
    log_info "The post-upgrade script is registered: $_DST_POSTUPGRADE_SCRIPT_PATH"
}

###########################################################
# MAIN                                                    #
###########################################################

if [[ ! -e "$POST_UPGRADE_SCRIPT" ]]; then
  # The file is required, so log "error" and exit with "error" when the file doesn't
  # exist.
  log_error "The $POST_UPGRADE_SCRIPT is required but do not exists."
  exit_error
fi

# install the post-upgrade script; modify the code as you wish
install_postupgrade_script

exit_fixed

